package com.pi.shisan.service.service.impl.next;

import com.pi.shisan.service.annotation.CalcNextTimeType;
import com.pi.shisan.service.constants.ClockTypeEnum;
import com.pi.shisan.service.dto.ClockInfoDTO;
import com.pi.shisan.service.service.ClockNextTimeService;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

@CalcNextTimeType(ClockTypeEnum.BETWEEN_TIME)
public class BetweenNextTimeServiceImpl implements ClockNextTimeService {
    @Override
    public Long calc(ClockInfoDTO clockInfoDTO) {
        Integer year = clockInfoDTO.getClkYear();
        Integer month = clockInfoDTO.getClkMonth();
        Integer day = clockInfoDTO.getClkDay();
        Integer hour = clockInfoDTO.getClkHour();
        Integer minute = clockInfoDTO.getClkMinute();
        long startTime = LocalDateTime.of(year,month,day,hour,minute,0).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        long now = System.currentTimeMillis();
        if (now > startTime) {
            startTime = now + (clockInfoDTO.getClkBetween() * 60000L - (now - startTime) / 60000L % clockInfoDTO.getClkBetween() * 60000L);
        }
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(startTime)));
        return startTime / 60000 * 60000;
    }
}
